package com.yuanda.wordhot.web.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yuanda.wordhot.entity.UserEntity;
import com.yuanda.wordhot.service.RoleService;
import com.yuanda.wordhot.service.UserService;
import com.yuanda.wordhot.util.JsonUtils;
import com.yuanda.wordhot.web.BaseController;

/**
 * 用户管理控制器
 * @author YuBai
 * @Description
 * @Date 2013-5-2
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/admin")
public class UserManagerController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	public ModelAndView showUsers() {
		return null;
	}
	
	@RequestMapping("/getUsers")
	@ResponseBody
	public String getUsers(int page,int rows){
		int start = (page - 1)*rows;
		return JsonUtils.generateJsonByObject(userService.getAllUsers(start,rows));
	}
	
	/**
	 * 增加用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/saveUser")
	@ResponseBody
	public String saveUser(HttpServletRequest request){
		UserEntity userEntity = bindUser(request);
		//System.out.println(request.getParameterValues("roles"));
//		user.setRegisterDate(new Date());
//		userService.addUser(user);
		return "用户创建成功";
	}
	
	private UserEntity bindUser(HttpServletRequest request) {
		UserEntity userEntity = new UserEntity();
		//TODO 增加把request里面数据取出来绑定到userEntity里面的代码
		return userEntity;
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateUser(UserEntity user){
		userService.updateUser(user);
		return "用户更新成功！";
	}
	
	@RequestMapping("/removeUser")
	@ResponseBody
	public String removeUser(long userId){
		userService.deleteUser(userId);
		return "用户删除成功！";
	}
}
